<?php
namespace app\modules\v1\controllers;
use Yii;
use yii\web\Response;
use yii\rest\ActiveController;
use app\modules\v1\models\Weixinupload;
use yii\base\Exception;
use app\modules\v1\models\Merchantextrastuff;
use app\modules\v1\models\MerchantWeixin;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;


class WeixinuploadController extends ActiveController
{
	
    public $modelClass = 'app\modules\v1\models\Weixinupload';
    
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['update'], $actions['create'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionIndex()
    {
    	$weixinupload = new Weixinupload();
    	try {
    		$res['data'] = $weixinupload->getSignPackage();
    		$res['code'] = '10000';
    		$res['msg']  = '成功';
    	} catch (Exception $e) {
    		$res['data'] = $e;
    		$res['code'] = '10400';
    		$res['msg']  = '失败';
    	}
    	$response=Yii::$app->response;
    	$response->format	= Response::FORMAT_JSON;
    	$response->data		= $res;
    	
    }
    
    public function actionCreate(){
    	$dirqiniu 		= Yii::$app->basePath.'/web/Qiniu/';
    	require_once $dirqiniu. 'autoload.php';
    	$accessKey 		= Yii::$app->params['accessKey'];
    	$secretKey 		= Yii::$app->params['secretKey'];
    	$auth 			= new Auth($accessKey, $secretKey);
    	$bmgr 			= new BucketManager($auth);
    	$img 			= isset(Yii::$app->request->post()['media_id'])?Yii::$app->request->post()['media_id']:'';
    	$openid 		= isset(Yii::$app->request->post()['openid'])?Yii::$app->request->post()['openid']:'';
    	if(empty($img) || empty($openid)){
    		$res['code'] = '10402';
    		$res['msg']  = '请取消关注后重新关注公众号';
    		$response			= Yii::$app->response;
    		$response->format	= Response::FORMAT_JSON;
    		$response->data		= $res;
    		return $response;
    	}else{
    		$upload		= new Weixinupload();
    		$url 		= $upload->getWeixinpicture($img);
    		$bucket 	= 'weixin';
    		$key 		= $img.'_'.time();
    		list($ret, $err) = $bmgr->fetch($url, $bucket, $key);
    		if ($err !== null) {
    			$res['code'] = '10400';
    			$res['msg']  = '系统忙，请稍后-';
    			$response			= Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		} else {
    			//保存数据到数据库
    			$weixininfo = MerchantWeixin::findByOpenid($openid);
    			if(!empty($weixininfo)){
    				$merchant_id	= $weixininfo->merchant_id;
    			}else{
    				$res['code'] = '10402';
		    		$res['msg']  = '请取消关注后重新关注公众号';
		    		$response			= Yii::$app->response;
		    		$response->format	= Response::FORMAT_JSON;
		    		$response->data		= $res;
		    		return $response;
    			}
    			try {
	    			$stuff   = new Merchantextrastuff();
	    			$stuff->merchant_id = $merchant_id;
	    			$stuff->extra_stuff = $key;
	    			$stuff->state = 0;
	    			$stuff->type  = 1;
	    			$stuff->weixin_id = $img;
	    			$stuff->save();
	    			$res['code'] = '10000';
	    			$res['msg']  = '上传成功';
	    			$response			= Yii::$app->response;
	    			$response->format	= Response::FORMAT_JSON;
	    			$response->data		= $res;
	    			return $response;
    			} catch (Exception $e) {
    				$res['code'] = '10400';
    				$res['msg']  = '上传失败，请稍后重试。';
    				$response			= Yii::$app->response;
    				$response->format	= Response::FORMAT_JSON;
    				$response->data		= $res;
    				return $response;
    			}
    		}
    	}	
    }
}